
package Product_Category;
import ConnnectData.DataConnect;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.modelTable;
/**
 *
 * @author langtustupid
 */
public class Product_CategoryShow extends javax.swing.JInternalFrame {
    /** Creates new form Product_Category */
    Connection con;
    Statement sta;
    ResultSet result;
    public Product_CategoryShow() {
        initComponents();
       con = DataConnect.connectDB();
    }
   

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtSearch = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbCategory_1 = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnEdit = new javax.swing.JMenu();
        mnAdd = new javax.swing.JMenu();
        mnDelete = new javax.swing.JMenu();
        mnShow = new javax.swing.JMenu();
        mnClose = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Product Category Management");

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 24));
        jLabel1.setText("Product Category Management");

        jLabel2.setText("Category Name :");

        txtSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearchKeyReleased(evt);
            }
        });

        tbCategory_1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbCategory_1.setRowHeight(20);
        tbCategory_1.setSelectionBackground(new java.awt.Color(255, 153, 255));
        jScrollPane1.setViewportView(tbCategory_1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 891, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(217, 217, 217)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(29, 29, 29)
                        .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(373, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addComponent(jLabel1)
                .addGap(36, 36, 36)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jMenuBar1.setBackground(new java.awt.Color(255, 255, 255));

        mnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/edit.JPG"))); // NOI18N
        mnEdit.setText(" Edit Category");
        mnEdit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnEditMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnEdit);

        mnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add.gif"))); // NOI18N
        mnAdd.setText(" Add Category");
        mnAdd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnAddMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnAdd);

        mnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/delete.png"))); // NOI18N
        mnDelete.setText("Delete Category ");
        mnDelete.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnDeleteMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnDelete);

        mnShow.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/star.gif"))); // NOI18N
        mnShow.setText("Show Category");
        mnShow.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnShowMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnShow);

        mnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/exit.png"))); // NOI18N
        mnClose.setText("Close");
        mnClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnCloseMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnClose);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnDeleteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnDeleteMouseClicked
         if (tbCategory_1.getSelectedRow() != -1) {
        int remove = JOptionPane.showConfirmDialog(this, "Are you sure want delete this Category !");
        if (remove == 0) {
            int index = Integer.parseInt(tbCategory_1.getValueAt(tbCategory_1.getSelectedRow(), 0).toString());
            try {
                String sql_delFK = "Delete from Product where Category_Id=" + index;
                sta.executeUpdate(sql_delFK);
                String sql_del = "Delete from Product_Category where Category_Id=" + index;
                sta.executeUpdate(sql_del);
                result = sta.executeQuery("Select *from Product_Category");
                tbCategory_1.setModel(new modelTable(result));
                JOptionPane.showMessageDialog(this, "Delete category success ! ");
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    } else {
        JOptionPane.showMessageDialog(null, "You should choose the Category want to delete !");
    }
    }//GEN-LAST:event_mnDeleteMouseClicked

    private void mnAddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnAddMouseClicked
        new AddProduct_catagory().setVisible(true);
    }//GEN-LAST:event_mnAddMouseClicked

    private void mnShowMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnShowMouseClicked
       try{
        sta = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        result = sta.executeQuery("Select * from Product_Category");
        tbCategory_1.setModel(new modelTable(result));
    }catch(Exception ex)
    {
        ex.printStackTrace();
    }
    }//GEN-LAST:event_mnShowMouseClicked

    private void mnEditMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnEditMouseClicked
        if(tbCategory_1.getSelectedRow()!=-1)
        {
        int Category_Id = Integer.parseInt(tbCategory_1.getValueAt(tbCategory_1 .getSelectedRow(), 0).toString());
        String Category_Name =(tbCategory_1.getValueAt(tbCategory_1.getSelectedRow(), 1).toString());
        String Category_Description =(tbCategory_1.getValueAt(tbCategory_1.getSelectedRow(), 2).toString());
        Update_Catagory frmUpdateCategory = new Update_Catagory();
        frmUpdateCategory.setCategory_Id(Category_Id);
        frmUpdateCategory.setCategory_Name(Category_Name);
        frmUpdateCategory.setCategory_Description(Category_Description);
        frmUpdateCategory.setVisible(true);}
        else
            JOptionPane.showMessageDialog(null, "You should choose the Category want to edit ! !");
    }//GEN-LAST:event_mnEditMouseClicked

    private void txtSearchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSearchKeyReleased
       try{
        result = sta.executeQuery("Select *from Product_Category where Category_Name like '%" + txtSearch.getText().trim() + "%'");
        tbCategory_1.setModel(new modelTable(result));
    }catch(Exception ex)
    {
        ex.printStackTrace();
    }
    }//GEN-LAST:event_txtSearchKeyReleased

private void mnCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnCloseMouseClicked
// TODO add your handling code here:
    int Close=JOptionPane.showConfirmDialog(null, "Are you sure want close  !");
       if(Close==0)
       this.dispose();
}//GEN-LAST:event_mnCloseMouseClicked

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Product_CategoryShow().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenu mnAdd;
    private javax.swing.JMenu mnClose;
    private javax.swing.JMenu mnDelete;
    private javax.swing.JMenu mnEdit;
    private javax.swing.JMenu mnShow;
    private javax.swing.JTable tbCategory_1;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables

}
